<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\User;
use App\Http\Model\Gift;
use App\Http\Model\GiftBag;

class RepairWealthCharm extends Command
{
    protected $signature = 'RepairWealthCharm';

    protected $description = '根据礼物流水修复财富值魅力值';

    private $redis_cache_finance_user_wallet = "CACHE:FINANCE:USER:WALLET";

    private $giftModel;
    private $giftBagModel;

    public function __construct()
    {
        parent::__construct();

        $this->giftModel = new Gift();
        $this->giftBagModel = new GiftBag();
    }

    public function handle()
    {
        do {
            $limit = 10000;
            for($i=0;$i<20;$i++) {
                $list = DB::table('gift_stream')
                    ->offset($i*$limit)
                    ->limit($limit)
                    ->get();
                $list = objToArray($list);
                foreach($list as $val) {
                    if($val['gift_type'] == 5) {
                        continue;
                    }
                    //房间财富值
                    if($val['room_id'] > 0) {
                        $roomWealth = DB::table('finance_room_wealth_stream')->where('gift_stream_id', $val['stream_id'])->first();
                        if(empty($roomWealth)) {
                            $log = [
                                'room_id' => $val['room_id'],
                                'user_id' => $val['from_user_id'],
                                'from_user_id' => $val['user_id'],
                                'wealth_type' => 3,
                                'wealth_value' => $val['gift_price']*$val['gift_num'],
                                'create_timestamp' =>  $val['change_timestamp'],
                                'create_time' =>  date('Y-m-d H:i:s', $val['change_timestamp']),
                                'gift_stream_id' =>  $val['stream_id'],
                            ];
                            DB::table('finance_room_wealth_stream')->insertGetId($log);
                        }
                    }
                    //用户财富值
                    $userWealth = DB::table('finance_wealth_stream')->where('gift_stream_id', $val['stream_id'])->first();
                    if(empty($userWealth)) {
                        $log = [
                            "user_id" => $val['from_user_id'],
                            "room_id" => $val['room_id'],
                            "wealth_value" => $val['gift_price']*$val['gift_num'],
                            "from_user_id" => $val['user_id'],
                            "change_timestamp" => $val['change_timestamp'],
                            "wealth_type" => 0,
                            'change_time' =>  date('Y-m-d H:i:s', $val['change_timestamp']),
                            "wealth_balance" => 0,
                            "room_wealth_value" => 0,
                            'gift_stream_id' =>  $val['stream_id'],
                        ];
                        DB::table('finance_wealth_stream')->insertGetId($log);
                    }
                    //用户魅力值
                    $userCharm = DB::table('finance_charm_stream')->where('gift_stream_id', $val['stream_id'])->first();
                    if(empty($userCharm)) {
                        $log = [
                            "user_id" => $val['user_id'],
                            "charm_value" => $val['gift_price']*$val['gift_num'],
                            "charm_desc" => '聊天室打赏-收获魅力值',
                            "change_timestamp" => $val['change_timestamp'],
                            'change_time' =>  date('Y-m-d H:i:s', $val['change_timestamp']),
                            "charm_type" => 0,
                            "room_id" => $val['room_id'],
                            "from_user_id" => $val['user_id'],
                            "charm_balance" => 0,
                            "will_be_deleted" => 0,
                            'gift_stream_id' =>  $val['stream_id'],
                        ];
                        DB::table('finance_charm_stream')->insertGetId($log);
                    }
                }
                echo $i."\r\n";
            }

            //用户钱包
            $userList = DB::table('user')->get();
            $userList = objToArray($userList);
            foreach($userList as $val) {
                if($val['user_id'] < 100) {
                    continue;
                }
                //用户财富值
                $userWealth = DB::table('finance_wealth_stream')->where('gift_stream_id', '>', 0)->where('user_id', $val['user_id'])->sum('wealth_value');
                //用户魅力值
                $userCharm = DB::table('finance_charm_stream')->where('gift_stream_id', '>', 0)->where('user_id', $val['user_id'])->sum('charm_value');

                DB::table("finance_wallet")->where('user_id', $val['user_id'])->update(['wealth_balance'=>$userWealth, 'charm_balance'=>$userCharm]);

                Redis::DEL($this->redis_cache_finance_user_wallet . ':' . $val['user_id']); //清除缓存
            }

            //房间钱包表
            $roomList = DB::table('room')->get();
            $roomList = objToArray($roomList);
            foreach($roomList as $val) {
                //房间财富值
                $roomWealth = DB::table('finance_room_wealth_stream')->where('gift_stream_id', '>', 0)->where('room_id', $val['room_id'])->sum('wealth_value');

                DB::table("finance_room_wallet")->where('room_id', $val['room_id'])->update(['wealth_balance'=>$roomWealth]);
            }

        } while (false);


        return true;
    }

}